<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/12/05 22:12:35 $)
// $RCSfile: functions_labels.php,v $ : $Revision: 1.2 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Insert a new Ticket Label
*/
function insertTicketLabel($title, $labeltype)
{
	global $dbCore;

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."ticketlabels` (`title`,`dateline`, `labeltype`, `staffid`) VALUES ('". $dbCore->escape($title) ."', '". DATENOW ."', '". $dbCore->escape($labeltype) ."', '". intval($_SWIFT["staff"]["staffid"]) ."');");

	rebuildTicketLabelCache();

	return true;
}

/**
* Update the Ticket Label
*/
function updateTicketLabel($ticketlabelid, $title, $labeltype)
{
	global $dbCore;

	$dbCore->query("UPDATE `". TABLE_PREFIX ."ticketlabels` SET `title` = '". $dbCore->escape($title) ."',`dateline` = '". DATENOW ."', `labeltype` = '". $dbCore->escape($labeltype) ."', `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."' WHERE `ticketlabelid` = '". intval($ticketlabelid) ."';");

	rebuildTicketLabelCache();

	return true;
}

/**
* Delete the given ticket labels
*/
function deleteTicketLabels($ticketlabelidlist)
{
	global $dbCore, $_SWIFT;

	if (!_is_array($ticketlabelidlist))
	{
		return false;
	}

	$_finalticketlabelidlist = array();
	$dbCore->query("SELECT `ticketlabelid` FROM `". TABLE_PREFIX ."ticketlabels` WHERE ((`labeltype` = '". SWIFTPUBLIC ."') OR (`labeltype` = '". SWIFTPRIVATE ."' AND `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."')) AND `ticketlabelid` IN (". buildIN($ticketlabelidlist) .");");
	while ($dbCore->nextRecord())
	{
		$_finalticketlabelidlist[] = $dbCore->Record["ticketlabelid"];
	}

	if (!count($_finalticketlabelidlist))
	{
		return false;
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."ticketlabels` WHERE `ticketlabelid` IN (". buildIN($_finalticketlabelidlist) .");");
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."ticketlabellinks` WHERE `ticketlabelid` IN (". buildIN($_finalticketlabelidlist) .");");

	rebuildTicketLabelCache();

	return true;
}

/**
* Rebuild the Ticket Label Cache
*/
function rebuildTicketLabelCache()
{
	global $dbCore, $_SWIFT, $datastore;

	$_cache = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."ticketlabels` ORDER BY `title` ASC;");
	while ($dbCore->nextRecord())
	{
		$_cache[$dbCore->Record["ticketlabelid"]] = $dbCore->Record;
	}

	$datastore->update("ticketlabelcache", $_cache);

	return true;
}
?>